<?php
/**
 * Created by PhpStorm.
 * User: mojie126
 * Date: 2017/6/8
 * Time: 下午4:43
 */

namespace app\index\controller;

use app\index\model\Category;
use app\index\model\Information;
use app\index\model\Users;

class SeriesController extends Base
{
	public function index()
	{
		$this->assign("title", "剧集");
		$list = db("series")->where("display", 0)->where("status", 0)->paginate(LIMIT, FALSE, ["type" => "app\common\Amazeui"]);
		$page = $list->render("c");
		$this->assign("page", $page);
		$this->getAccess();

		$whereOr = "";
		$name = input("?name");
		if ($name) {
			$name = input("name");
			$whereOr = [
				"maintitle" => ["LIKE", "%$name%"],
				"subtitle" => ["LIKE", "%$name%"],
				"doubanurl" => ["=", "$name"],
				"imdburl" => ["=", "$name"]
			];
		}

		$where = "";
		$olcate = input("?olcate");
		$slcate = input("?slcate");
		$official = input("?official");
		$first = input("?first");
		$excl = input("?excl");
		$bump = input("?bump");
		if ($olcate) {
			$olcate = input("olcate");
			$where = [
				"olcate" => ["=", $olcate]
			];
		}
		if ($slcate) {
			$slcate = input("slcate");
			$where = [
				"slcate" => ["=", $slcate]
			];
		}
		if ($official) {
			$where = [
				"official" => ["=", "1"]
			];
		}
		if ($first) {
			$where = [
				"first" => ["=", "1"]
			];
		}
		if ($excl) {
			$where = [
				"excl" => ["=", "1"]
			];
		}
		if ($bump) {
			$where = [
				"bump" => ["=", "1"]
			];
		}

		$info = input("page", "");
		$res = db("series")
			->where($where)
			->where("display", 0)
			->where("status", 0)
			->whereOr($whereOr)
			->page($info, LIMIT)
			->order("sticklevel DESC, addtime DESC")->select();
		$rss = db("rss")->where("userid", session("uid"))->select();
		foreach ($res as $k => $v) {
			$res[$k]['ymd'] = date("Y-m-d", strtotime($v['addtime']));
			$res[$k]['his'] = date("H:i:s", strtotime($v['addtime']));
			$res[$k]['rss'] = "0";
			$userinfo = Users::getUserInfo($v['uploader']);
			if ($res[$k]['anonymous'] == 0) {
				$res[$k]['uploadername'] = $userinfo['username'];
			} elseif ($this->checkAccess(1)) {
				$res[$k]['uploadername'] = $userinfo['username'];
			} else {
				$res[$k]['uploadername'] = "<i>匿名</i>";
			}
			if ($rss) {
				foreach ($rss as $key) {
					if ($key['torrentid'] == $v['id'] && $key['type'] == $v['type']) {
						$res[$k]['rss'] = "1";
					}
				}
			}
		}
		$userinfo = Users::getUserInfo(session("uid"));
		$this->assign("userinfo", $userinfo);
		$this->assign("series", $res);
		db("users")->where("uid", session("uid"))->update(["serieslasttime" => nowtime()]);

		return $this->fetch();
	}

	public function details($id)
	{
		$this->assign("title", "详情");
		$torrentinfo = db("series")->where("id", $id)->find();
		$torrentinfo['olcate'] = Category::getCategoryName($torrentinfo['olcate']);
		$torrentinfo['slcate'] = Category::getCategoryName($torrentinfo['slcate']);
		$torrentinfo['author'] = Information::getAuthor($torrentinfo['author']);
		$torrentinfo['discount_name'] = discount($torrentinfo['discount']);
		$torrentinfo['uploadername'] = Users::getUserName($torrentinfo['uploader']);
		$torrentinfo['type'] = "s";
		$this->assign("t", $torrentinfo);
		$userinfo = Users::getUserInfo(session("uid"));
		$this->assign("u", $userinfo);
		$n = input("?n");
		if ($n) {
			header("refresh: 1; url=" . getWebURL() . "/dl/s/$id/$userinfo[uuid]");
		}
		$this->getAccess();

		return $this->fetch("Details/Index");
	}

	public function releaseName($name)
	{
		$this->assign("title", $name);
		$res = db("series")->where("albumname", $name)->group("subtitle")->order("addtime", "DESC")->field("subtitle")->select();
		$field = ["id", "maintitle", "seeder", "leecher", "completed", "torrentsize", "container", "resolution", "source", "type", "imdburl", "imdbscore", "logscore"];
		$rss = db("rss")->where("userid", session("uid"))->select();
		foreach ($res as $k => $v) {
			$res[$k]['uuid'] = Users::getUserUUID(session("uid"));
			$res[$k]['son'] = db("series")->where("subtitle", $v['subtitle'])->order("addtime", "DESC")->field($field)->select();
			$res[$k]['son_num'] = count($res[$k]['son']);
			foreach ($res[$k]['son'] as $key => $value) {
				$res[$k]['son'][$key]['rss'] = "0";
				if ($rss) {
					foreach ($rss as $rss_key) {
						if ($rss_key['torrentid'] == $value['id'] && $rss_key['type'] == $value['type']) {
							$res[$k]['son'][$key]['rss'] = "1";
						}
					}
				}
			}
		}
		$this->assign("series", $res);

		return $this->fetch();
	}
}